//
//  LTQHomeTableViewCell.swift
//  WeiBoProject
//
//  Created by MAC on 16/3/2.
//  Copyright © 2016年 LTQ. All rights reserved.
//

import UIKit
import SnapKit

//    微博Cell 中控件的间距数值
let StatusCellMargin: CGFloat = 10
//    微博头像的宽度
let StatusCellIconWidth:CGFloat = 35

///    微博Cell
class LTQHomeTableViewCell: UITableViewCell {
    
    var constant: Constraint?
    
    var statusViewModel:LTQStatusViewModel?{
        didSet{
            orginalView.statusViewModel = statusViewModel
            footerView.footerViewModel = statusViewModel
            // cell有重用导致了约束加重,
            // 先清空约束
            constant?.uninstall()
            if statusViewModel?.statusModel?.retweeted_status == nil{
                // 没有转发微博
                
                /*
                
                修改这个约束 make.top.equalTo(retweetView.snp_bottom)
                隐藏转发微博视图
                */
                
                retweetView.hidden = true
                footerView.snp_updateConstraints(closure: { (make) -> Void in
                    self.constant = make.top.equalTo(orginalView.snp_bottom).constraint
                })
            }else{
                
                          // 有转发微博
            /*
            修改这个约束 make.top.equalTo(retweetView.snp_bottom)
            隐藏转发微博视图
            传值
            */
            retweetView.hidden = false
            retweetView.retweetViewModel = statusViewModel
            footerView.snp_updateConstraints(closure: { (make) -> Void in
                self.constant = make.top.equalTo(retweetView.snp_bottom).constraint
            })
            }
        }
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setupUI(){
        
        contentView.addSubview(orginalView)
        contentView.addSubview(footerView)
        contentView.addSubview(retweetView)
        orginalView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left)
            make.top.equalTo(contentView.snp_top)
            make.right.equalTo(contentView.snp_right)
        
        }
        footerView.snp_makeConstraints { (make) -> Void in
            
            self.constant = make.top.equalTo(retweetView.snp_bottom).constraint
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.bottom.equalTo(contentView.snp_bottom)
            make.height.equalTo(40)
        }
        retweetView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left)
            make.top.equalTo(orginalView.snp_bottom)
            make.right.equalTo(contentView.snp_right)

        }
        
        //什么把视图,放值到contentView
        /*
        contentView是根据自动计算的左右上下
        一定注意
        */
        contentView.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(self.snp_left)
            make.top.equalTo(self.snp_top)
            make.right.equalTo(self.snp_right)
            
            make.bottom.equalTo(footerView.snp_bottom)
            
        }
        
    }
    
    private lazy var orginalView:LTQOrginal = LTQOrginal()
    
    private lazy var footerView:LTQFooterView = LTQFooterView()
    
    private lazy var retweetView:LTQRetweetView = LTQRetweetView()
    
}
